package service

import (
	"gomain/app/model"

	"github.com/gogf/gf/v2/frame/g"
)

var Member = memberService{}

type memberService struct{}

func (s *memberService) List(page, limit int) ([]model.Member, int, error) {
	var members []model.Member

	m := g.DB().Model("members")
	count, err := m.Count()
	if err != nil {
		return nil, 0, err
	}

	err = m.Page(page, limit).Scan(&members)
	return members, count, err
}

func (s *memberService) Add(member *model.Member) error {
	_, err := g.DB().Model("members").Data(member).Insert()
	return err
}

func (s *memberService) Update(member *model.Member) error {
	_, err := g.DB().Model("members").Data(member).Where("id=?", member.Id).Update()
	return err
}

func (s *memberService) Delete(id int) error {
	_, err := g.DB().Model("members").Where("id=?", id).Delete()
	return err
}
